<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/4/21
 * Time: 12:41 PM
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;

use app\api\model\ProductOrderContent;
use app\api\model\Store as StoreModel;
use app\api\model\ProductOrder as ProductOrderModel;
use app\api\model\StoreUser;
use app\api\model\TeamUser;
use app\api\model\User;
use app\api\model\UserMoney;
use app\lib\exception\ParameterException;
use app\api\service\Token;

class Store extends BaseController
{
    protected $beforeActionList = [
        'checkPrimaryScope' => ['only' => 'getDataList']
    ];
    /**
     * 获取店面列表
     */
    public function getDataList()
    {
        return StoreModel::order('order_sort desc')->select()->toArray();
    }
    

    
    /**
     * 核销订单查询
     */
    public function getOrderInfo($scene)
    {
        $uid = Token::getCurrentUid();
        $storeUser = StoreUser::where('user_id',$uid)->find();
        $scene = explode("-",$scene);
        $order = ProductOrderModel::where('id',$scene[1])->with('content.img')->find();
        if(!$order) {
            throw new ParameterException([
                'msg' => [
                    'order'=> '订单为空'
                ]
            ]);
        } else {
            if($storeUser['store_id'] != $order['store_id']) {
                throw new ParameterException([
                    'msg' => [
                        'order'=> '订单非本门店'
                    ]
                ]);
            }
            return $order;
        }
    }

    /**
     * 核销订单
     */
    public function setOrder($order_id)
    {
        $uid = Token::getCurrentUid();
        $storeUser = StoreUser::where('user_id',$uid)->find();
        $order = ProductOrderModel::get($order_id);
        if(!$order) {
            throw new ParameterException([
                'msg' => [
                    'order'=> '订单为空'
                ]
            ]);
        } else {
            if($storeUser['store_id'] != $order['store_id']) {
                throw new ParameterException([
                    'msg' => [
                        'order'=> '订单非本门店'
                    ]
                ]);
            }
            $orderCon = ProductOrderContent::where('product_order_id',$order['id'])
                ->with('product')
                ->find();
            if($order['status'] != 2) {
                throw new ParameterException([
                    'msg' => [
                        'order'=> '订单状态错误'
                    ]
                ]);
            }

            ProductOrderModel::where('id',$order_id)->update([
                'he_time' => time(),
                'store_user_id' => $storeUser['id'],
                'status' => 3
            ]);
            ProductOrderContent::where('product_order_id',$order['id'])->update([
                'status' => 3
            ]);
            if($order['order_type'] == 2) {
                $money = $orderCon['product']['new_user_pay_cash']*$orderCon['product_number'];
                if($money > 0) {
                    $userInfo = User::where('id',$order['user_id'])->find();
                    User::where('id',$order['user_id'])->setDec('money',$money);
                    UserMoney::create([
                        'order_id' => $order['id'],
                        'user_id' => $userInfo['id'],
                        'user_type' => 1,
                        'type' => 1,
                        'user_money' => $userInfo['money'],
                        'money' => $money,
                        'status' => 5
                    ]);
                }
            }
            return writeJson(202, [], '核销成功！');
        }
    }
}